package com.wwwarehouse.carddesk.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;

import java.util.List;

public class FunctionDetailsAdapter extends BaseAdapter {
    private Context mContext;
    private List<CardDeskFunctionResponseBean.TaskBean> mCardDeskFunctionList;
    private int mItemHeight;

    public FunctionDetailsAdapter(Context context, List<CardDeskFunctionResponseBean.TaskBean> cardDeskFunctionList, int itemHeight) {
        this.mContext = context;
        this.mCardDeskFunctionList = cardDeskFunctionList;
        this.mItemHeight = itemHeight;
    }

    @Override
    public int getCount() {
        return mCardDeskFunctionList == null ? 0 : mCardDeskFunctionList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCardDeskFunctionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final CardDeskFunctionResponseBean.TaskBean functionBean = mCardDeskFunctionList.get(position);
        final ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_function_details, null);
            holder = new ViewHolder();
            holder.rootLayout = (LinearLayout) convertView.findViewById(R.id.ll_root);
            holder.iconImg = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.newImg = (ImageView) convertView.findViewById(R.id.iv_new);
            holder.nameTxt = (TextView) convertView.findViewById(R.id.tv_name);

            convertView.setTag(holder);

            AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.height = mItemHeight;
            holder.rootLayout.setLayoutParams(params);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

       String taskImgUrl = functionBean.getTaskImgUrl();
       if (!TextUtils.isEmpty(taskImgUrl)) {
           BaseApplication.getApplicationInstance().displayImg(functionBean.getTaskImgUrl(), holder.iconImg);
       }

       if ("1".equals(functionBean.getIsClick())) {
           holder.newImg.setVisibility(View.VISIBLE);
       } else {
           holder.newImg.setVisibility(View.GONE);
       }

       holder.nameTxt.setText(functionBean.getTaskName());



        return convertView;
    }

    private class ViewHolder {
        private LinearLayout rootLayout;

        private ImageView iconImg;
        private ImageView newImg;
        private TextView nameTxt;
    }
}